package cn.edu.zut.hhr.controller;

import cn.edu.zut.hhr.annotation.CacheAnnotation;
import cn.edu.zut.hhr.annotation.LogAnnotation;
import cn.edu.zut.hhr.service.LoginService;
import cn.edu.zut.hhr.vo.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 虽然查询要用GET,但登录要是也GET密码啥的就丢光了...
     * @return
     */
    @PostMapping(value = "/login")
    @LogAnnotation(module = "登录",operator = "登录")
    /**
     * 千万别对登录进行缓存,不然用户信息的缓存比登录的缓存先过期的话就等死吧...
     */
    public RestResponse login(
            @RequestParam("username") Long username,
            @RequestParam("password") String password
    ){
        String token = loginService.login(username,password);
        if(token != null){
            return RestResponse.success(token);
        }
        return RestResponse.fail(401,"用户名或密码错误");
    }

    @PostMapping("/logout")
    @LogAnnotation(module = "登录",operator = "登出")
    public RestResponse logout(@RequestHeader("token") String token){
        loginService.logout(token);
        return RestResponse.success(null);
    }

}
